<template>
  <vue-content>
    <vue-header>
      <vue-header-form>
        <el-form :inline="true" :model="formFilter" ref="formFilter">
          <el-form-item label="商户名称" prop="mchtName">
            <el-input v-model.trim="formFilter.mchtName"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="info" @click="SearchSubmit('formFilter')">查询</el-button>
            <el-button @click="resetForm('formFilter')">重置</el-button>
          </el-form-item>
        </el-form>
      </vue-header-form>
      <vue-header-button>
        <el-button type="info" @click="create">新增</el-button>
      </vue-header-button>
    </vue-header>
    <vue-body>
      <el-table
        v-loading.body="loading"
        :data="tableData"
        border
        style="width: 100%">
        <el-table-column
          prop="mchtName"
          label="名称">
        </el-table-column>
        <el-table-column
          label="状态" width="90">
          <template slot-scope="scope">
            {{ scope.row.mchtStatus | mchtStatus }}
          </template>
        </el-table-column>
        <el-table-column
          prop="enterDate"
          label="入住时间" width="180">
        </el-table-column>
        <!--<el-table-column
          prop="mchtDetails"
          label="详情">
        </el-table-column>-->
        <el-table-column
          prop="userName"
          label="管理员名称" width="160">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button-group>
            <el-button
              :disabled="scope.row.mchtStatus === '0'?true:false"
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
              v-show = "scope.row.mchtStatus === '0'"
              size="mini"
              type="danger"
              @click="enabled(scope.row.mchtId)">禁用</el-button>
            <el-button
            	v-show = "scope.row.mchtStatus === '1'"
              size="mini"
              @click="abled(scope.row.mchtId)">启用</el-button>
              <el-button
              size="mini"
              type="info"
              @click="handleDetail(scope.$index, scope.row, tableData)">详情</el-button>
              <el-button
             :disabled="scope.row.roleType === '0'?true:false"
              size="mini"
              @click="resetPwd(scope.$index, scope.row)" >重置密码</el-button>
              </el-button-group>
          </template>
      </el-table-column>
      </el-table>
    </vue-body>
    <vue-footer style="padding: 5px 0;margin: 0 -5px;float:right;">
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="10"
        layout="total, prev, pager, next"
        :total="pageSize">
      </el-pagination>
    </vue-footer>
    <edit-role
      ref="edit-role"
      @close="editFinish"
      :view="view"
      v-if="view ==='create' || view ==='edit'"/>
      <edit-detail
      ref="edit-detail"
      @close="editFinish"
      :view="view"
      v-if="view ==='detail'"/>
      <reset-user-pwd
      ref="reset-pwd"
      :view="view"
      @close="editFinish"
      v-if="view==='reset'"/>
  </vue-content>
</template>

<script>
  import {query, forbidden, use, queryDetail} from '@/services/Merchant'
  import {RequestError} from '@/services/errors'
  import {deepClone} from '@/utilities/object'
  import EditRole from './edit'
  import EditDetail from './Detail'
  import resetUserPwd from './reset-pwd'
  export default {
    name: 'role-table',
    components: {EditRole,EditDetail,resetUserPwd},
    data: () => ({
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      formFilter: {
        mchtName: ""
      },
      loading: false,
      view: ''
    }),
    methods: {
      async loadData () {
        this.loading = true
        try {
          let data = await query(Object.assign({currentPage: this.currentPage},{perPage:10}, this.formFilter))
          // debugger
//        console.log(data)
          this.tableData = data.data
          this.pageSize = parseInt(data.totalResult)
        } catch (e) {
          if (e instanceof RequestError) {
            this.$alert(e.message, '提示', {
              type: 'error',
              confirmButtonText: '确定'
            })
          } else {
            throw e
          }
        }
        this.loading = false
      },
      async  create () {
        this.view = 'create'
        await this.$nextTick()
        this.$refs['edit-role'].dialogFormVisible = true
      },
      async handleEdit (index, data) {
        this.view = 'edit'
        await this.$nextTick()
        this.$refs['edit-role'].dialogFormVisible = true
        this.$refs['edit-role'].formData = deepClone(data)
        this.$refs['edit-role'].userId = data.userId
      },
      async resetPwd (index, data) {
        this.view = 'reset'
        await this.$nextTick()
        try {
          let dataDetail = await queryDetail(data.mchtId)
          // debugger
          this.$refs['reset-pwd'].formData = deepClone(data)
          this.$refs['reset-pwd'].formData.id = dataDetail.userId
        } catch (e) {
          if (e instanceof RequestError) {
            this.$alert(e.message, '提示', {
              type: 'error',
              confirmButtonText: '确定'
            })
          } else {
            throw e
          }
        }
      },
      async enabled (mchtId) {
        try {
          await forbidden(mchtId)
          this.$message({
            type: 'success',
            message: '成功'
          })
          this.loadData()
        } catch (e) {
          if (e instanceof RequestError) {
            this.$alert(e.message, '提示', {
              type: 'error',
              confirmButtonText: '确定'
            })
          } else {
            throw e
          }
        }
      },
      async abled (mchtId) {
        try {
          await use(mchtId)
          this.$message({
            type: 'success',
            message: '成功'
          })
          this.loadData()
        } catch (e) {
          if (e instanceof RequestError) {
            this.$alert(e.message, '提示', {
              type: 'error',
              confirmButtonText: '确定'
            })
          } else {
            throw e
          }
        }
      },
      async handleDetail (index, data) {
        this.view = 'detail'
        await this.$nextTick()
        this.$refs['edit-detail'].mchtId = data.mchtId
        this.$refs['edit-detail'].formData = data
        this.$refs['edit-detail'].loadData()
      },
      SearchSubmit () {
        this.currentPage = 1
        this.loadData()
      },
      resetForm (formName) {
        this.$refs[formName].resetFields()
        this.currentPage = 1
        this.loadData()
      },
      editFinish (needRefresh) {
        this.view = ''
        if (needRefresh) {
          this.loadData()
        }
      }
    },
    watch: {
      currentPage (newVal) {
        this.loadData(newVal)
      }
    },
    filters: {
      mchtStatus (value) {
        return {'0': '启用', '1': '禁用'}[value]
      }
    },
    created () {
      this.loadData()
    }
  }
</script>
